package com.translator.ui;

import com.translator.config.AppConfig;
import com.translator.util.ProxyManager;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 状态栏组件
 * 显示应用程序状态信息，如翻译状态、代理状态等
 */
public class StatusBar extends JPanel {
    
    private JLabel statusLabel;
    private JLabel proxyLabel;
    private JLabel timeLabel;
    
    /**
     * 构造函数
     */
    public StatusBar() {
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5));
        setPreferredSize(new Dimension(getWidth(), 25));
        
        // 创建状态标签
        statusLabel = new JLabel("就绪");
        statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
        
        // 创建代理状态标签
        proxyLabel = new JLabel();
        proxyLabel.setHorizontalAlignment(SwingConstants.CENTER);
        updateProxyStatus();
        
        // 创建时间标签
        timeLabel = new JLabel();
        timeLabel.setHorizontalAlignment(SwingConstants.RIGHT);
        updateTime();
        
        // 创建定时器更新时间
        Timer timer = new Timer(1000, e -> updateTime());
        timer.start();
        
        // 添加组件
        JPanel leftPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
        leftPanel.setOpaque(false);
        leftPanel.add(statusLabel);
        
        JPanel centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
        centerPanel.setOpaque(false);
        centerPanel.add(proxyLabel);
        
        JPanel rightPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
        rightPanel.setOpaque(false);
        rightPanel.add(timeLabel);
        
        add(leftPanel, BorderLayout.WEST);
        add(centerPanel, BorderLayout.CENTER);
        add(rightPanel, BorderLayout.EAST);
    }
    
    /**
     * 更新状态信息
     * 
     * @param status 状态信息
     */
    public void setStatus(String status) {
        statusLabel.setText(status);
    }
    
    /**
     * 更新代理状态信息
     */
    public void updateProxyStatus() {
        AppConfig config = AppConfig.getInstance();
        if (config.isProxyEnabled()) {
            proxyLabel.setText("代理已启用: " + config.getProxyHost() + ":" + config.getProxyPort());
            proxyLabel.setForeground(new Color(0, 128, 0)); // 绿色
        } else {
            proxyLabel.setText("代理未启用");
            proxyLabel.setForeground(Color.GRAY);
        }
    }
    
    /**
     * 更新时间信息
     */
    private void updateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        timeLabel.setText(sdf.format(new Date()));
    }
}